package com.github.sevenheaven.gesturelock.widget.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.github.sevenheaven.R;
import com.github.sevenheaven.gesturelock.widget.GestureLock;
import com.github.sevenheaven.gesturelock.widget.GestureLockView;
import com.github.sevenheaven.gesturelock.widget.GestureLockUtils;


public class LockActivity extends ActionBarActivity {
    public static final int UNLOCK_FAILED = 1115;
    private GestureLock gestureView;
    private int errorCount = 5;
    private TextView mHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);

        getSupportActionBar().hide();

        mHint = ((TextView) findViewById(R.id.unlock_hint));
        //GestureLockUtils.setDrawTrack(LockActivity.this, true);

        gestureView = (GestureLock) findViewById(R.id.gesture_lock);
        gestureView.setCorrectGesture(GestureLockUtils.getGesture(this));
        gestureView.setMode(GestureLockView.ARROW_BOTTOM);

        gestureView.setOnGestureEventListener(new GestureLock.OnGestureEventListener() {

            @Override
            public void onGestureEvent(boolean matched) {
                // Toast.makeText(LockActivity.this, "Match:" + matched, Toast.LENGTH_SHORT).show();
                if (matched) {
                    gestureView.invalidate();
                    GestureLockUtils.setLocked(false);
                    LockActivity.this.finish();
                } else {
                    errorCount--;
                    mHint.setTextColor(getResources().getColor(R.color.app_red));
                    mHint.setText("您的输入有误，还有" + errorCount + "次机会。");
                }
            }

            @Override
            public void onUnmatchedExceedBoundary() {
                // TODO 处理5次输入失败
                setResult(UNLOCK_FAILED);
                GestureLockUtils.setLocked(false);
                finish();
            }

            @Override
            public void onBlockSelected(int position) {
//                Log.d("position", position + "");
            }


        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.action_settings) {
            GestureLockUtils.setDrawTrack(this, !GestureLockUtils.isDrawTrack(this));
            Toast.makeText(this, "draw pattern" + GestureLockUtils.isDrawTrack(this), Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        Toast.makeText(this, "应用已锁定，请输入手势解锁", Toast.LENGTH_SHORT).show();
    }

}
